package com.hiq.psycho;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Main{
	Intellect brain;
	
	private static final String RUS_VERSION = "rus";
	
	private static final String COMP = "Dr. Smith: ";
	private static final String YOU = "You: ";
	
	JFrame win;
	JTextArea answer;
	JTextField quest;
	JButton ok;


	public static void main(String [] args){
	
	int mode = 0;
		for (int i=0; i<args.length; i++){
			if (args[i].equals(RUS_VERSION)){
				mode = 1;
			}
		}
		new Main(mode);
	}
	
	public Main(int i){
		brain = new Intellect(i);
		createAndSetGui();
	}
	
	public void createAndSetGui(){
		win = new JFrame("Your Psychologist");
		win.setSize(800, 500);
		win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		win.setLayout(new BorderLayout());
		
		quest = new JTextField();
		quest.setBackground(Color.DARK_GRAY);
		quest.setForeground(Color.WHITE);
		quest.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e) {
				if ('\n' == e.getKeyChar()){
					performSubmit();
				};
			}
		});
		answer = new JTextArea();
		answer.setEnabled(false);
		answer.setBackground(Color.DARK_GRAY);
		answer.setDisabledTextColor(Color.WHITE);
		ok = new JButton("SAY");
		
		JPanel p1 = new JPanel(new BorderLayout());;
		JPanel p2 = new JPanel(new BorderLayout());
		
		JScrollPane sp = new JScrollPane(answer);
		
		p1.add(sp);
		
		p2.add(quest, BorderLayout.CENTER);
		p2.add(ok, BorderLayout.EAST);
		
		win.add(p1, BorderLayout.CENTER);
		win.add(p2, BorderLayout.SOUTH);
		
		ok.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				performSubmit();
			}
		}
		);
		
		win.setVisible(true);
		
	}
	
	private void performSubmit(){
		String q = quest.getText();
		brain.setPreviousAnswer(q);
		String old = answer.getText();
		q = YOU + q;
		String a = brain.answer(q);
		a = COMP + a;
		answer.setText(old + q + "\n" + a + "\n");
		quest.setText("");
		

	}
	
}